$SAVE
$NOLIST
;************************************************************************
;*									*
;*    R T X - 51  :  MACROS: INSERT TASK IN READY LIST                  *
;*							     		*
;*----------------------------------------------------------------------*
;*                                                                      *
;*    Filename     :   RTXINS.INC                                       *
;*    Language     :   Keil A-51                                        *
;*    Dev. system  :   Keil uVision2                                    *
;*    Targetsystem :   Any system based upon 8051 up                    *
;*                                                                      *
;*    Purpose      :   Insert specified task id at the end or at the    *
;*                     beginning of the ready list. To be used as an    *
;*                     include file.                                    *
;*								     	*
;*----------------------------------------------------------------------*
;* Rev. |  Released   | Programmer    | Comments                        *
;*----------------------------------------------------------------------*
;* 0.1  |  4.1.1990   | E. Glatz      | For V3.18                       *
;* 0.2  | 10.4.1991   | Th. Fischler  | Adapted to Keil A-51,           *
;*      |             |               | Macro INSERT_RDY_PRIO splitted  *
;*      |             |               | to INSERT_RDY_PRIO and          *
;*      |             |               | INSERT_RDY_PRIO_W_A (Akku holds *
;*      |             |               | the Task-ID)                    *
;*      |  7.5.1991   |               | P2-Addressing eliminated        *
;* 7.00 | 13.11.2001  | T. Mueller    | Release 7.00                    *
;************************************************************************
;*    Copyright 1991 .. 2001 METTLER  &  FUCHS  AG,  CH-8953 Dietikon   *
;************************************************************************

INSERT_RDY_PRIO   MACRO    TASK_ID
       LOCAL PRIO0, PRIO1, PRIO23, PRIO2, PRIO3, ENDL
;;
;;       Insert task at the end of the ready list, task-id in RAM-variable
;;       -----------------------------------------------------------------
;;
;;       Inserts the task, the task_id of it is contained in an
;;       int. RAM variable (ACC not possible), at the correct position 
;;       depending on its priority in the ready-list.
;;       Supported are 4 different task priorities and max. 16 active tasks.
;;
;;       Format:
;;
;;               INSERT_RDY_PRIO Parameter
;;
;;               Parameter: The name of a int. RAM variable (must
;;                          be bit-addressable).
;;
;;       SIDE EFFECTS :  uses ACC and DPTR
;;
;
;       INSERT_RDY_PRIO  TASK_ID
;
               MOV   DPH,#HIGH(?RTX_SYS_PAGE)        ; Select RAM page 0
               JB    TASK_ID.7, PRIO23          ; Insert task_id in
               JB    TASK_ID.6, PRIO1           ; correct prio. FIFO

PRIO0:         INC   ?RTX_RDY_COUNT0                 ; Insert task_id in
               MOV   DPL, ?RTX_RDY_LAST0             ; FIFO 0
               MOV   A, TASK_ID
               MOVX  @DPTR, A
               ; increment pointer and test for overflow
               INC   ?RTX_RDY_LAST0
               MOV   A, ?RTX_RDY_LAST0
               CJNE  A, #LOW(?RTX_RDY_PRIO0_END), ENDL
               MOV   ?RTX_RDY_LAST0, #LOW(?RTX_RDY_PRIO0)
               SJMP  ENDL

PRIO1:         INC   ?RTX_RDY_COUNT1                 ; Insert task_id in
               MOV   DPL, ?RTX_RDY_LAST1             ; FIFO 1
               MOV   A, TASK_ID
               MOVX  @DPTR, A
               ; increment pointer and test for overflow
               INC   ?RTX_RDY_LAST1
               MOV   A, ?RTX_RDY_LAST1
               CJNE  A, #LOW(?RTX_RDY_PRIO1_END), ENDL
               MOV   ?RTX_RDY_LAST1, #LOW(?RTX_RDY_PRIO1)
               SJMP  ENDL

PRIO23:        JB    TASK_ID.6, PRIO3

PRIO2:         INC   ?RTX_RDY_COUNT2                 ; Insert task_id in
               MOV   DPL, ?RTX_RDY_LAST2             ; FIFO 2
               MOV   A, TASK_ID
               MOVX  @DPTR, A
               ; increment pointer and test for overflow
               INC   ?RTX_RDY_LAST2
               MOV   A, ?RTX_RDY_LAST2
               CJNE  A, #LOW(?RTX_RDY_PRIO2_END), ENDL
               MOV   ?RTX_RDY_LAST2, #LOW(?RTX_RDY_PRIO2)
               SJMP  ENDL

PRIO3:         INC   ?RTX_RDY_COUNT3                 ; Insert task_id in
               MOV   DPL, ?RTX_RDY_LAST3             ; FIFO 3
               MOV   A, TASK_ID
               MOVX  @DPTR, A
               ; increment pointer and test for overflow
               INC   ?RTX_RDY_LAST3
               MOV   A, ?RTX_RDY_LAST3
               CJNE  A, #LOW(?RTX_RDY_PRIO3_END), ENDL
               MOV   ?RTX_RDY_LAST3, #LOW(?RTX_RDY_PRIO3)
               SJMP  ENDL

ENDL:
               ENDM




INSERT_RDY_PRIO_W_A  MACRO
       LOCAL PRIO0, PRIO1, PRIO23, PRIO2, PRIO3, ENDL
;;
;;       Insert task at the end of the ready list, task-id in Akku
;;       ---------------------------------------------------------
;;
;;       Inserts the task, the task_id of it is contained in ACC,
;;       at the correct position depending on its priority in the
;;       ready-list.
;;       Supported are 4 different task priorities and max. 16 active tasks.
;;
;;       Format:
;;
;;               INSERT_RDY_PRIO_W_A
;;
;;
;;       SIDE EFFECTS :  uses ACC and DPTR
;;
;
;       INSERT_RDY_PRIO_W_A
;
               MOV   DPH,#HIGH(?RTX_SYS_PAGE)        ; Select RAM page 0
               JB    ACC.7, PRIO23              ; Insert task_id in
               JB    ACC.6, PRIO1               ; correct prio. FIFO

PRIO0:         INC   ?RTX_RDY_COUNT0                 ; Insert task_id in
               MOV   DPL, ?RTX_RDY_LAST0             ; FIFO 0
               MOVX  @DPTR, A
               ; increment pointer and test for overflow
               INC   ?RTX_RDY_LAST0
               MOV   A, ?RTX_RDY_LAST0
               CJNE  A, #LOW(?RTX_RDY_PRIO0_END), ENDL
               MOV   ?RTX_RDY_LAST0, #LOW(?RTX_RDY_PRIO0)
               SJMP  ENDL

PRIO1:         INC   ?RTX_RDY_COUNT1                 ; Insert task_id in
               MOV   DPL, ?RTX_RDY_LAST1             ; FIFO 1
               MOVX  @DPTR, A
               ; increment pointer and test for overflow
               INC   ?RTX_RDY_LAST1
               MOV   A, ?RTX_RDY_LAST1
               CJNE  A, #LOW(?RTX_RDY_PRIO1_END), ENDL
               MOV   ?RTX_RDY_LAST1, #LOW(?RTX_RDY_PRIO1)
               SJMP  ENDL

PRIO23:        JB    ACC.6, PRIO3

PRIO2:         INC   ?RTX_RDY_COUNT2                 ; Insert task_id in
               MOV   DPL, ?RTX_RDY_LAST2             ; FIFO 2
               MOVX  @DPTR, A
               ; increment pointer and test for overflow
               INC   ?RTX_RDY_LAST2
               MOV   A, ?RTX_RDY_LAST2
               CJNE  A, #LOW(?RTX_RDY_PRIO2_END), ENDL
               MOV   ?RTX_RDY_LAST2, #LOW(?RTX_RDY_PRIO2)
               SJMP  ENDL

PRIO3:         INC   ?RTX_RDY_COUNT3                 ; Insert task_id in
               MOV   DPL, ?RTX_RDY_LAST3             ; FIFO 3
               MOVX  @DPTR, A
               ; increment pointer and test for overflow
               INC   ?RTX_RDY_LAST3
               MOV   A, ?RTX_RDY_LAST3
               CJNE  A, #LOW(?RTX_RDY_PRIO3_END), ENDL
               MOV   ?RTX_RDY_LAST3, #LOW(?RTX_RDY_PRIO3)
               SJMP  ENDL

ENDL:
               ENDM




REINSERT_RDY_PRIO    MACRO    TASK_ID
    LOCAL PRIO0, PRIO1, PRIO23, PRIO2, PRIO3, ENDL, DEC0, DEC1, DEC2, DEC3
;;
;;       Insert task at the beginning of the ready list
;;       ----------------------------------------------
;;
;;       Inserts the task, the task_id of it is contained in an int. RAM
;;       variable (ACC not possible), at the correct position depending on
;;       its priority in the ready-list.
;;       Supported are 4 different task priorities and max. 16 active tasks.
;;
;;       Format:
;;
;;               REINSERT_RDY_PRIO Parameter
;;
;;               Parameter: name of an int. RAM variable (must be
;;                          bit-addressable).
;;
;;       SIDE EFFECTS :  uses ACC and DPTR
;;
;
;       REINSERT_RDY_PRIO  TASK_ID
;
               MOV   DPH, #HIGH(?RTX_SYS_PAGE)       ; Select RAM page 0
               JB    TASK_ID.7, PRIO23          ; Insert task_id in
               JB    TASK_ID.6, PRIO1           ; correct prio. FIFO

PRIO0:         INC   ?RTX_RDY_COUNT0
               MOV   A, ?RTX_RDY_FIRST0              ; Insert task_id in
               DEC   A                          ; FIFO 0
               CJNE  A, #LOW(?RTX_RDY_PRIO0-1), DEC0
               MOV   A, #LOW(?RTX_RDY_PRIO0_END-1)
DEC0:          MOV   DPL, A
               MOV   ?RTX_RDY_FIRST0, A
               MOV   A, TASK_ID
               MOVX  @DPTR, A
               SJMP  ENDL

PRIO1:         INC   ?RTX_RDY_COUNT1
               MOV   A, ?RTX_RDY_FIRST1              ; Insert task_id in
               DEC   A                          ; FIFO 1
               CJNE  A, #LOW(?RTX_RDY_PRIO1-1), DEC1
               MOV   A, #LOW(?RTX_RDY_PRIO1_END-1)
DEC1:          MOV   DPL, A
               MOV   ?RTX_RDY_FIRST1, A
               MOV   A, TASK_ID
               MOVX  @DPTR, A
               SJMP  ENDL

PRIO23:        JB    TASK_ID.6, PRIO3

PRIO2:         INC   ?RTX_RDY_COUNT2
               MOV   A, ?RTX_RDY_FIRST2              ; Insert task_id in
               DEC   A                          ; FIFO 2
               CJNE  A, #LOW(?RTX_RDY_PRIO2-1), DEC2
               MOV   A, #LOW(?RTX_RDY_PRIO2_END-1)
DEC2:          MOV   DPL, A
               MOV   ?RTX_RDY_FIRST2, A
               MOV   A, TASK_ID
               MOVX  @DPTR, A
               SJMP  ENDL

PRIO3:         INC   ?RTX_RDY_COUNT3
               MOV   A, ?RTX_RDY_FIRST3              ; Insert task_id in
               DEC   A                          ; FIFO 3
               CJNE  A, #LOW(?RTX_RDY_PRIO3-1), DEC3
               MOV   A, #LOW(?RTX_RDY_PRIO3_END-1)
DEC3:          MOV   DPL, A
               MOV   ?RTX_RDY_FIRST3, A
               MOV   A, TASK_ID
               MOVX  @DPTR, A
ENDL:
               ENDM

$RESTORE
